﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Corsat.Library.Logic;
using Corsat.Library;
using Microsoft.Reporting.WinForms;
using Corsat.Desktop.Reports;

namespace Corsat.Desktop
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Controls.Add(new UserControls.ucSearch());
            //Controls[0].Dock = DockStyle.Fill;

            //Course_ToolStripMenuItem.DropDownItems.Add("qwe", null, new EventHandler((x, y) => Test(sender, e, 1)));

            List<CourseInReports> lstCourse = Reporting.GetAllCoursesForReporting();

            for (int i = 0; i < lstCourse.Count; i++)
            {
                CourseInReports temp = lstCourse[i];


                if (temp.CourseId == 0)
                {
                    SeatNumbers_ResultsReady_ToolStripMenuItem.DropDownItems.Add(temp.CourseName);
                    SeatNumbers_w_Signature_ToolStripMenuItem.DropDownItems.Add(temp.CourseName);
                    SeatNumbers_wo_Signature_ToolStripMenuItem.DropDownItems.Add(temp.CourseName);
                    Results_ToolStripMenuItem.DropDownItems.Add(temp.CourseName);
                    //Calls_ToolStripMenuItem.DropDownItems.Add(temp.CourseName);

                    for (int j = 0; j < temp.Levels.Count; j++)
                    {
                        int CourseId = temp.Levels[j].Id;
                        string CourseName = temp.CourseName + " - " + temp.Levels[j].Value;

                        ((ToolStripMenuItem)SeatNumbers_ResultsReady_ToolStripMenuItem.DropDownItems[i]).DropDownItems.Add(temp.Levels[j].Value, null, new EventHandler((x, y) => SeatNumbers(sender, e, CourseId, CourseName, true, true)));
                        ((ToolStripMenuItem)SeatNumbers_w_Signature_ToolStripMenuItem.DropDownItems[i]).DropDownItems.Add(temp.Levels[j].Value, null, new EventHandler((x, y) => SeatNumbers(sender, e, CourseId, CourseName, true, false)));
                        ((ToolStripMenuItem)SeatNumbers_wo_Signature_ToolStripMenuItem.DropDownItems[i]).DropDownItems.Add(temp.Levels[j].Value, null, new EventHandler((x, y) => SeatNumbers(sender, e, CourseId, CourseName, false, false)));
                        ((ToolStripMenuItem)Results_ToolStripMenuItem.DropDownItems[i]).DropDownItems.Add(temp.Levels[j].Value, null, new EventHandler((x, y) => Results(sender, e, CourseId, CourseName, false)));
                        //((ToolStripMenuItem)Calls_ToolStripMenuItem.DropDownItems[i]).DropDownItems.Add(temp.Levels[j].Value, null, new EventHandler((x, y) => Results(sender, e, CourseId, CourseName, true)));
                    }
                }
                else
                {
                    SeatNumbers_ResultsReady_ToolStripMenuItem.DropDownItems.Add(temp.CourseName, null, new EventHandler((x, y) => SeatNumbers(sender, e, temp.CourseId, temp.CourseName, true, true)));
                    SeatNumbers_w_Signature_ToolStripMenuItem.DropDownItems.Add(temp.CourseName, null, new EventHandler((x, y) => SeatNumbers(sender, e, temp.CourseId, temp.CourseName, true, false)));
                    SeatNumbers_wo_Signature_ToolStripMenuItem.DropDownItems.Add(temp.CourseName, null, new EventHandler((x, y) => SeatNumbers(sender, e, temp.CourseId, temp.CourseName, false, false)));
                    Results_ToolStripMenuItem.DropDownItems.Add(temp.CourseName, null, new EventHandler((x, y) => Results(sender, e, temp.CourseId, temp.CourseName, false)));
                    //Calls_ToolStripMenuItem.DropDownItems.Add(temp.CourseName, null, new EventHandler((x, y) => Results(sender, e, temp.CourseId, temp.CourseName, true)));
                }

            }
            this.reportViewer1.RefreshReport();
        }

        protected void SeatNumbers(object sender, EventArgs e, int CourseId, string CourseName, bool WithSign, bool IsForResult)
        {
            if (IsForResult)
                reportViewer1.LocalReport.ReportPath = "Reports\\SeatNumbers_ResultsReady.rdlc";
            else
            {
                if (WithSign)
                    reportViewer1.LocalReport.ReportPath = "Reports\\SeatNumbers_WithSignature.rdlc";
                else
                    reportViewer1.LocalReport.ReportPath = "Reports\\SeatNumbers.rdlc";
            }
            reportViewer1.LocalReport.DataSources.Clear();
            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Accounts", Reporting.GetAccounts(CourseId)));
            reportViewer1.LocalReport.SetParameters(new ReportParameter("CourseName", CourseName));
            reportViewer1.RefreshReport();
        }

        protected void Results(object sender, EventArgs e, int CourseId, string CourseName, bool IncludeTelephones)
        {
            if (IncludeTelephones)
                reportViewer1.LocalReport.ReportPath = "Reports\\CallsOnResults.rdlc";
            else
                reportViewer1.LocalReport.ReportPath = "Reports\\Results.rdlc";

            reportViewer1.LocalReport.DataSources.Clear();
            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Results", Reporting.GetResults(CourseId, IncludeTelephones)));
            reportViewer1.LocalReport.SetParameters(new ReportParameter("CourseName", CourseName));
            reportViewer1.LocalReport.DisplayName = CourseName;
            reportViewer1.RefreshReport();

        }

        private void الأرشيفToolStripMenuItem_Click(object sender, EventArgs e)
        {
            reportViewer1.LocalReport.ReportPath = "Reports\\Archive.rdlc";
            reportViewer1.LocalReport.DataSources.Clear();
            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Archive", Reporting.GetArchiveReport()));

            reportViewer1.RefreshReport();
        }

        private void الشهاداتToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            reportViewer1.LocalReport.ReportPath = "Reports\\CertificatesReport.rdlc";
            reportViewer1.LocalReport.DataSources.Clear();
            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", Reporting.GetAllCertificates()));

            reportViewer1.RefreshReport();
        }

        private void الأغلفةToolStripMenuItem_Click(object sender, EventArgs e)
        {
            reportViewer1.LocalReport.ReportPath = "Reports\\CertificateEnvelopesReport.rdlc";
            reportViewer1.LocalReport.DataSources.Clear();
            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet", Reporting.GetAllCertificates()));

            reportViewer1.RefreshReport();
        }
    }
}
